﻿using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using RotovapPlatform.Domain.Equipment.DeviceParameters;

namespace RotovapPlatform.Domain.Services.DeviceParameters
{
	/// <summary>
	/// 设备参数领域服务实现
	/// </summary>
	public class DeviceParameterDomainService : IDeviceParameterDomainService
	{
		private readonly IDeviceParameterService _deviceParameterService;
		private readonly ILogger<DeviceParameterDomainService> _logger;
		private DeviceParametersEntity _deviceParameters;

		/// <summary>
		/// 构造函数
		/// </summary>
		public DeviceParameterDomainService(IDeviceParameterService deviceParameterService, ILogger<DeviceParameterDomainService> logger)
		{
			_deviceParameterService = deviceParameterService;
			_logger = logger;
		}

		/// <summary>
		/// 初始化设备参数
		/// </summary>
		public async Task InitializeAsync()
		{
			try
			{
				_deviceParameters = await _deviceParameterService.GetParametersAsync();
				_logger.LogInformation("设备参数领域服务初始化成功");
			}
			catch (Exception ex)
			{
				_logger.LogError(ex, "设备参数领域服务初始化失败");
				_deviceParameters = new DeviceParametersEntity();
			}
		}

		/// <summary>
		/// 获取工艺参数范围
		/// </summary>
		public async Task<(double Min, double Max, string Unit)> GetProcessParameterRangeAsync(string parameterName)
		{
			if (_deviceParameters == null)
			{
				await InitializeAsync();
			}

			if (_deviceParameters?.ProcessParameters == null || !_deviceParameters.ProcessParameters.ContainsKey(parameterName))
			{
				_logger.LogWarning($"未找到工艺参数: {parameterName}，使用默认范围");
				return (0, 100, "");
			}

			var parameter = _deviceParameters.ProcessParameters[parameterName];
			return (parameter.MinValue, parameter.MaxValue, parameter.Unit);
		}

		/// <summary>
		/// 获取油浴温度范围
		/// </summary>
		public async Task<(double Min, double Max, string Unit)> GetOilTemperatureRangeAsync()
		{
			return await GetProcessParameterRangeAsync("oilBathTemperature");
		}

		/// <summary>
		/// 获取旋蒸转速范围
		/// </summary>
		public async Task<(double Min, double Max, string Unit)> GetRotarySpeedRangeAsync()
		{
			return await GetProcessParameterRangeAsync("rotationSpeed");
		}

		/// <summary>
		/// 获取阶梯温度范围
		/// </summary>
		public async Task<(double Min, double Max, string Unit)> GetStepTemperatureRangeAsync()
		{
			return await GetProcessParameterRangeAsync("oilBathTemperature");
		}

		/// <summary>
		/// 获取阶梯稳定时间范围
		/// </summary>
		public async Task<(double Min, double Max, string Unit)> GetStepStableTimeRangeAsync()
		{
			return await GetProcessParameterRangeAsync("processTime");
		}

		/// <summary>
		/// 获取旋蒸时间范围
		/// </summary>
		public async Task<(double Min, double Max, string Unit)> GetRotaryTimeRangeAsync()
		{
			return await GetProcessParameterRangeAsync("processTime");
		}

		/// <summary>
		/// 获取冷凝温度范围
		/// </summary>
		public async Task<(double Min, double Max, string Unit)> GetCondensationTemperatureRangeAsync()
		{
			return await GetProcessParameterRangeAsync("coolingTemperature");
		}

		/// <summary>
		/// 获取真空度范围
		/// </summary>
		public async Task<(double Min, double Max, string Unit)> GetVacuumRangeAsync()
		{
			return await GetProcessParameterRangeAsync("vacuumPressure");
		}

		/// <summary>
		/// 获取清洗体积范围
		/// </summary>
		public async Task<(double Min, double Max, string Unit)> GetCleaningVolumeRangeAsync()
		{
			// 使用自定义范围，因为JSON中没有直接对应的参数
			if (_deviceParameters == null)
			{
				await InitializeAsync();
			}

			if (_deviceParameters?.Pumps == null || _deviceParameters.Pumps.Count == 0)
			{
				_logger.LogWarning("未找到泵参数，使用默认范围");
				return (0, 100, "ml");
			}

			// 使用冷凝管上方洗液泵的流量作为参考
			var pump = _deviceParameters.Pumps.Find(p => p.Id == "CondenserWashPump");
			if (pump == null)
			{
				_logger.LogWarning("未找到冷凝管上方洗液泵参数，使用默认范围");
				return (0, 100, "ml");
			}

			// 假设清洗体积范围是0到泵的最大流量的10倍
			return (0, pump.Parameters.FlowRate * 10, "ml");
		}

		/// <summary>
		/// 获取清洗速度范围
		/// </summary>
		public async Task<(double Min, double Max, string Unit)> GetCleaningSpeedRangeAsync()
		{
			// 使用自定义范围，因为JSON中没有直接对应的参数
			if (_deviceParameters == null)
			{
				await InitializeAsync();
			}

			if (_deviceParameters?.Pumps == null || _deviceParameters.Pumps.Count == 0)
			{
				_logger.LogWarning("未找到泵参数，使用默认范围");
				return (0, 50, "ml/min");
			}

			// 使用冷凝管上方洗液泵的流量作为参考
			var pump = _deviceParameters.Pumps.Find(p => p.Id == "CondenserWashPump");
			if (pump == null)
			{
				_logger.LogWarning("未找到冷凝管上方洗液泵参数，使用默认范围");
				return (0, 50, "ml/min");
			}

			return (0, pump.Parameters.MaxSpeed, pump.Parameters.Unit);
		}

		/// <summary>
		/// 获取清洗时间范围
		/// </summary>
		public async Task<(double Min, double Max, string Unit)> GetCleaningTimeRangeAsync()
		{
			// 使用处理时间的范围作为参考
			var timeRange = await GetProcessParameterRangeAsync("processTime");
			// 清洗时间通常比处理时间短
			return (0, Math.Min(timeRange.Max, 30), "min");
		}
	}
}